#include "../include/tips.h"

struct jpg_node
{
    struct list_head list;  // 链表节点
    char pathname[64];      // 存储图片路径
};

struct list_head jpg_head;

void init_lists() 
{
    INIT_LIST_HEAD(&jpg_head);
}

void head_insert(struct list_head *head, const char *pathname) 
{
    struct jpg_node *new_node = (struct jpg_node *)malloc(sizeof(struct jpg_node));
    if (!new_node) 
    {
        fprintf(stderr, "new node malloc failed\n");
        return;
    }
    strncpy(new_node->pathname, pathname, sizeof(new_node->pathname) - 1);
    new_node->pathname[sizeof(new_node->pathname) - 1] = '\0'; // 确保字符串以 null 结尾
    list_add(&new_node->list, head); // 在链表头部插入新节点
}

void display_images() 
{
    struct jpg_node *p;

    // 初始化指针到链表的第一个节点
    p = list_entry(jpg_head.next, struct jpg_node, list);

    while (1) 
    {
        lcd_draw_jpg(0, 240, p->pathname, NULL, 0, 0);
        usleep(2000 * 1000);

        // 移动到下一个节点
        p = list_entry(p->list.next, struct jpg_node, list);

        // 如果回到链表头部，跳到下一个节点
        if (&p->list == &jpg_head) 
        {
            p = list_entry(jpg_head.next, struct jpg_node, list);
        }
    }
}

void* tips_display(void *arg)
{
    pthread_detach(pthread_self());

    init_lists();

    head_insert(&jpg_head, "6-1.jpg");
    head_insert(&jpg_head, "6-2.jpg");
    head_insert(&jpg_head, "6-3.jpg");
    head_insert(&jpg_head, "6-4.jpg");
    head_insert(&jpg_head, "6-5.jpg");

    display_images();
}